home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: in1.uu.net!iglou!news
- From: "Abe L. Getchell" <panther@iglou.com>
- Subject: BC++ Inheritance Question...
- X-Nntp-Posting-Host: dp-2-63.iglou.net
- Content-Type: text/plain; charset=us-ascii
- Message-ID: <311D9DBE.9E5@iglou.com>
- Sender: news@iglou.com (News Administrator)
- Content-Transfer-Encoding: 7bit
- Organization: 3DX Studios
- Mime-Version: 1.0
- Date: Sun, 11 Feb 1996 07:41:50 GMT
- X-Mailer: Mozilla 2.0 (Win16; I)
-
- I am new to OOP and have a question about a block of code that
- I wrote and was having problems with. In public inheritance, the private
- data mambers of a class stay private to the base class, and are not inherited
- by the derived class. Now, in this code, it appears that the derived class is
- inheriting the private data member "z" of the base class. I could just be interperating
- it wrong, or programming it wrong, but I cannot figure it out... please post to me here,
- or send mail to my address. I appriciate any help. (code and output given below)
-
- -----------------------------------------------------------------------------------------
- // Testing class inheritance
-
- #include <iostream.h>
- #include <conio.h>
-
- // Class definition & function definitions for ClassOne
- class ClassOne {
- public:
- ClassOne( int = 1, int = 2, int = 3 );
- int geta( void ) { return a; };
- int getb( void ) { return b; };
- int getz( void ) { return z; };
- int setz( void ) { z = 99 ; };
- protected:
- int a;
- int b;
- private:
- int z;
- };
-
- ClassOne::ClassOne( int x, int y, int q )
- {
- a = x;
- b = y;
- z = q;
- }
-
- // Class definition & function definitions for ClassTwo
- class ClassTwo : public ClassOne {
- public:
- ClassTwo( int = 4, int = 5 );
- int getc( void ) { return c; };
- int getd( void ) { return d; };
- protected:
- int c;
- int d;
- };
-
- ClassTwo::ClassTwo( int q, int p )
- {
- c = q;
- d = p;
- }
-
- main()
- {
- ClassOne oneObj;
- ClassTwo twoObj;
-
- cout << "ClassOne a = " << oneObj.geta() << "\n";
- cout << "ClassOne b = " << oneObj.getb() << "\n";
- cout << "ClassOne z = " << oneObj.getz() << "\n";
- cout << "ClassTwo a = " << twoObj.geta() << "(inherited from ClassOne)\n";
- cout << "ClassTwo b = " << twoObj.getb() << "(inherited from ClassOne)\n";
- cout << "ClassTwo c = " << twoObj.getc() << "\n";
- cout << "ClassTwo d = " << twoObj.getd() << "\n";
- cout << "ClassTwo z = " << twoObj.getz() << "\n";
- oneObj.setz();
- cout << "ClassTwo z = " << twoObj.getz() << "\n";
- cout << "ClassOne z = " << oneObj.getz() << "\n";
-
- getche();
-
- return 0;
- }
- ---------------------------------------------------------------------------------------
- ---------------------------------------------------------------------------------------
- ClassOne a = 1
- ClassOne b = 2
- ClassOne z = 3
- ClassTwo a = 1 (inherited from ClassOne)
- ClassTwo b = 2 (inherited from ClassOne)
- ClassTwo c = 4
- ClassTwo d = 5
- ClassTwo z = 3
- ClassTwo z = 3
- ClassOne z = 99
- ----------------------------------------------------------------------------------------
-
- Thanks in advance...
-
- Abe L. Getchell
-